package com.alan.wifishare.view.impl.activity;

import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import com.alan.libapi.business.dao.CallbackListener;
import com.alan.libapi.model.business.UserSession;
import com.alan.libapi.model.persistent.ErrorSets;
import com.alan.library.allog.KLog;
import com.alan.wifishare.R;
import com.alan.wifishare.app.GlobalField;
import com.alan.wifishare.app.UserConfig;
import com.alan.wifishare.utils.BusinessUtil;
import com.alan.wifishare.view.BaseActivity;
import com.alan.wifishare.view.impl.ActivityJumpUtil;
import com.alan.wifishare.view.impl.service.BusinessService;

/**
 * @className: LaunchActivity
 * @describtion: LaunchActivity
 * @author: AlanWang
 * @email: 850267876@qq.com
 * @time: 2016/5/5 15:51
 */
public class LaunchActivity extends BaseActivity{

    private Handler handler = new Handler();
    private boolean isToLoginAct = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.launch_layout);
        initView();
        tryLogin();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
//                BusinessService.startService(LaunchActivity.this);
                ActivityJumpUtil.jump2MainAct(LaunchActivity.this);
//                if(isToLoginAct){
//                    ActivityJumpUtil.jump2UserLoginAct(LaunchActivity.this);
//                }else {
//                    ActivityJumpUtil.jump2MainAct(LaunchActivity.this);
//                }
                LaunchActivity.this.finish();
            }
        }, 1500);
    }

    @Override
    protected void initView() {
    }

    @Override
    protected void onDestroy() {
//        BusinessService.stopService(this);
        super.onDestroy();
    }

    private void tryLogin(){

        String userAccount = GlobalField.userConfig.getUserAccount();
        String userPwd = GlobalField.userConfig.getUserPwd();
        KLog.e("userAccount = " + userAccount);
        KLog.e("userPwd = " + userPwd);
        KLog.e("isNetworkConnected = " + BusinessUtil.isNetworkConnected(GlobalField.context));
        if(TextUtils.isEmpty(userAccount) || TextUtils.isEmpty(userPwd)){
           isToLoginAct = true;
            return;
        }
        GlobalField.businessHttp.postUserLogin(userAccount, userPwd, new CallbackListener<UserSession>() {
            @Override
            public void onSuccess(UserSession result) {
                GlobalField.userSession = result;
                isToLoginAct = false;
            }

            @Override
            public void onFailure(int errorCode, String msg) {
                //如果帐号不存在或密码错误则跳转至登录界面
                KLog.e(msg);
                if(errorCode == ErrorSets.CODE_USER_LOGIN_ACCOUNT_NOT_EXIST
                        || errorCode == ErrorSets.CODE_USER_LOGIN_PWD_ERROR){
                    isToLoginAct = true;
                }else {
                    isToLoginAct = false;
                }
            }
        });
    }

}
